<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
    <title>收件箱</title>
    <%@ include file="../../include/head.jsp"%>
    <link rel="stylesheet" type="text/css" href="${ctxStatic}/3rd-lib/wangEditor/css/wangEditor.min.css">
    <style type="text/css">
        span.close{
            color: #dd514c;
            position: absolute;
            margin-top: -13px;
            margin-left: 4px;
            width: 14px;
            height: 14px;
        }
    </style>
</head>
<body>
<div class="admin-content">
    <div class="am-g">
        <div class="am-u-sm-12">
            <form id="mform" class="am-form am-form-horizontal" data-am-validator modelAttribute="msgSend" method="post">
                <input type="hidden" name="id" value="${msgSend.id}"/>
                <input type="hidden" name="type" value="${msgSend.type}"/>
                <input type="hidden" id="users" name="users" value="${msgSend.users}"/>
                <div class="am-form-group">
                    <label class="am-u-sm-2 am-form-label">发送给：</label>
                    <div class="am-u-sm-9" id="usersHtml" style="margin-top:10px;">
                        <button type="button" class="am-btn am-btn-primary am-btn-xs" onclick="openModel('选择用户','${ctx}/user/selectUser?users=${msgSend.users}','700px','420px')"><span class="am-icon-plus"></span></button>
                    </div>
                    <label class="am-u-sm-1"></label>
                </div>
                <div class="am-form-group">
                    <label class="am-u-sm-2 am-form-label">标题：</label>
                    <div class="am-u-sm-9">
                        <input type="text" name="title" placeholder="请输入标题" value="${msgSend.title}" required/>
                    </div>
                    <label class="am-u-sm-1"></label>
                </div>
                <div class="am-form-group">
                    <label class="am-u-sm-2 am-form-label">内容：</label>
                    <div class="am-u-sm-9">
                        <textarea id="content" name="content" rows="15" placeholder="请输入内容..." style="display: none;">
                            ${not empty msgSend.content?msgSend.content:''}
                        </textarea>
                    </div>
                    <label class="am-u-sm-1"></label>
                </div>
                <div class="am-form-group">
                    <label class="am-u-sm-2 am-form-label">级别：</label>
                    <div class="am-u-sm-9">
                        <select name="levels" data="${msgSend.levels}">
                            <option value="0">普通</option>
                            <option value="1">高</option>
                            <option value="-1">低</option>
                        </select>
                    </div>
                    <label class="am-u-sm-1"></label>
                </div>
                <div class="am-form-group">
                    <div class="am-u-sm-9 am-u-sm-push-2">
                        <button type="button" id="save" class="am-btn am-btn-primary">保存草稿</button>
                        <button type="button" id="send" class="am-btn am-btn-success">发送</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<%@ include file="../../include/bottom.jsp"%>
<script type="text/javascript" src="${ctxStatic}/3rd-lib/wangEditor/js/wangEditor.min.js"></script>
<script type="text/javascript" src="${ctxStatic}/3rd-lib/common/js/jq.editor.js"></script>
<script type="text/javascript" src="${ctxStatic}/3rd-lib/tpl/tpl.js"></script>
<!-- 定义模板 -->
<script type="text/template" id="usersTpl" desc="发送给模板">
    <# for(var i=0; i<list.length; i++) {var item=list[i];#>
    <button type="button" class="am-btn am-btn-primary am-btn-xs"><#=item.name #><span class="close" onclick="removeUser('<#=item.id #>',this)"><span class="am-icon-remove"></span></span></button>
    <#}#>
        <button type="button" class="am-btn am-btn-primary am-btn-xs" onclick="openModel('选择用户','${ctx}/user/selectUser?users=<#=ids #>','700px','420px')"><span class="am-icon-plus"></span></button>
</script>
<script type="text/javascript">
    //当前地址
    var cctx = '${pageContext.request.contextPath}';
    $(document).ready(function () {
        //消息提醒
        var msg = '${msg}';
        if (msg != '') {
            iuMsg(msg);
            closeModel(true);//关闭窗口
        }
        initSelectValue(true);//初始化下拉框的值
        //初始化编辑器
        $.fn.initEditor('${ctx}','${ctxStatic}','content');

        //绑定事件
        $("#save").click(function(){
            //保存草稿
            var users = $("#users").val().split(",");
            if(users.length==0 || users[0]==""){
                iuMsg('请选择发送给谁');
                return;
            }
            var content = $("#content").val();
            if(content=="" || content.replace(/(^\s*)|(\s*$)/g, "")==""){
                iuMsg('请输入内容');
                return;
            }
            var $form = $("#mform");
            $form.attr("action","${ctx}/msgSend/save");
            $form.submit();
        });
        $("#send").click(function(){
            //发送信息
            var users = $("#users").val().split(",");
            if(users.length==0 || users[0]==""){
                iuMsg('请选择发送给谁');
                return;
            }
            var content = $("#content").val();
            if(content=="" || content.replace(/(^\s*)|(\s*$)/g, "")==""){
                iuMsg('请输入内容');
                return;
            }
            var $form = $("#mform");
            $form.attr("action","${ctx}/msgSend/send");
            $form.submit();
        });
    });
    //选择用户后回调函数
    function callSelectUser(ids){
        $("#users").val(ids);
        get("${ctx}/user/getUsers?users="+ids,function(data){
            if(data.ret==1 && data.data){
                $("#usersHtml").html(tpl('#usersTpl', {ids:ids,list:data.data}));
            }
        });
    }
    callSelectUser('${msgSend.users}');
    //删除用户
    function removeUser(id,o){
        $(o).parent().remove();
        var users = $("#users").val().split(",");
        var ids = "";
        for(var i=0;i<users.length-1;i++){
            if(users[i]!=id){
                ids+=users[i]+",";
            }
        }
        callSelectUser(ids);
    }
</script>
</body>
</html>
